//
//  NewFeatureViewController.swift
//  Weibo
//
//  Created by 梁智 on 15/11/26.
//  Copyright © 2015年 lzfree. All rights reserved.
//

import UIKit
import SnapKit

/// 可重用 CellId
private let WBNewFeatureViewCellId = "WBNewFeatureViewCellId"
/// 新特性图像的数量
private let WBNewFeatureImageCount = 4


class NewFeatureViewController: UICollectionViewController {
    
    // MARK: - 构造函数
    init () {
        let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        
        super.init(collectionViewLayout: layout)
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func prefersStatusBarHidden() -> Bool {
        return true
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: WBNewFeatureViewCellId)
        

    }

    // MARK: - UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return WBNewFeatureImageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WBNewFeatureViewCellId, forIndexPath: indexPath) as! NewFeatureCell
    
        cell.imageIndex = indexPath.item
    
        return cell
    }
    
    // ScrollView 停止滚动方法
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        // 再最后一页才调用动画方法
        // 根据 contentOffset 计算页数
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        // 判断是否是最后一页
        if page != WBNewFeatureImageCount - 1 {
            return
        }
        
        // Cell 播放动画
        let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0)) as! NewFeatureCell
        
        // 显示动画
        cell.showButtonAnim()
    }

}

// MARK: - 新特性 cell
private class NewFeatureCell: UICollectionViewCell {
    
    /// 图像属性
    private var imageIndex: Int = 0 {
        didSet {
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            
            startButton.hidden = true
        }
    }
    
    /// 点击开始体验按钮
    @objc private func clickStartButton() {
        print("开始体验")
        // 发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: nil)
    }
    
    private func showButtonAnim() {
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        startButton.userInteractionEnabled = false
        
        UIView.animateWithDuration(2.0,
            delay: 0,
            usingSpringWithDamping: 0.6,    // 弹力系数，0~1，越小越弹
            initialSpringVelocity: 10,      // 初始速度，模拟重力加速度
            options: [],
            animations: { () -> Void in
                
                self.startButton.transform = CGAffineTransformIdentity
                
            }) { (_) -> Void in
                
                self.startButton.userInteractionEnabled = true
        }
    }
    
    // frame 的大小是 layout.itemSize 决定的
    override init(frame: CGRect) {
        super.init(frame: frame)
        print("创建 cell")
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        // 1. 添加控件
        addSubview(iconView)
        addSubview(startButton)
        
        // 不能单纯在此设置隐藏
        startButton.hidden = true
        
        // 2. 指定位置
        iconView.frame = bounds
        
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.bottom.equalTo(self.snp_bottom).multipliedBy(0.7)
        }
        
        // 3. 监听方法
        startButton.addTarget(self, action: "clickStartButton", forControlEvents: .TouchUpInside)
    }
    
    // MARK: - 懒加载控件
    /// 图像
    private lazy var iconView: UIImageView = UIImageView()
    /// 开始体验按钮
    private lazy var startButton: UIButton = UIButton(title: "开始体验", titleColor: UIColor.whiteColor(), backImageName: "new_feature_finish_button")
    
}